﻿using System;
using System.Data;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.BLL;
using Maticsoft.Model;


namespace FineUI.Examples
{
    public partial class teacher_update : System.Web.UI.Page
    {



        private void SetTeacherName()
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }

            string CurrentTeaUserName = Session["UserName"].ToString();
            Maticsoft.BLL.Teacher CurrentTeacher = new Maticsoft.BLL.Teacher();
            Maticsoft.Model.Teacher CurrentTeaInfom = CurrentTeacher.GetModel(CurrentTeaUserName);
            labTeacherName.Text = CurrentTeaInfom.T_Name.ToString();
        }


        protected void SetDefulatInform()
        {

            datepickerStart.Text = Session["R_STime"].ToString();
            datepickeEnd.Text = Session["R_ETime"].ToString();

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            int flag =Convert.ToInt32( Request.QueryString.Get("a"));
           
           
            if (!IsPostBack)
            {

                SetTeacherName();
                LoadTeachingCourseInform();
                SetDefulatInform();
            }

           
            if (flag > 0)
            {
                btnCreateDoc.Text = "修改报告";
            }
            

        }

        protected DataSet LoadTeachingCourseInform()
        {



            Maticsoft.BLL.ReportTemplate DocTemplate = new Maticsoft.BLL.ReportTemplate();
            string currentT_Id = Session["UserName"].ToString();

            DataSet DSTemplate = DocTemplate.GetTeachingClassCourseName("TeachingClass.T_Id='" + currentT_Id + "' order by TC_Name");
            //  DataSet DSTemplate = DocTemplate.GetList("TeachingClass.TC_Id=ReportTemplate.TC_Id and TeachingClass.T_Id='" + currentT_Id + "'");

            DataView CurrentView = DSTemplate.Tables[0].DefaultView;
            DropDownListCourse.DataTextField = "TC_Name";
            DropDownListCourse.DataValueField = "TC_Id";
            DropDownListCourse.DataSource = CurrentView;
            DropDownListCourse.DataBind();

            return DSTemplate;

        }


        protected void btnCreateDoc_Click(object sender, EventArgs e)
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^.+\.(doc)|(docx)$");
            if (!regex.IsMatch(fileUploadDoc.FileName))
            {
                Alert.Show("不支持该文件格式", MessageBoxIcon.Information);

            }
            else
            {
                Maticsoft.BLL.ReportTemplate documentation = new Maticsoft.BLL.ReportTemplate();
                Maticsoft.Model.ReportTemplate documentInfom = new Maticsoft.Model.ReportTemplate();
                TimeSpan document_time = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                fileUploadDoc.SaveAs(Server.MapPath(@"~/document/teacher/") + "\\" + Convert.ToInt64(document_time.TotalSeconds).ToString() + fileUploadDoc.ShortFileName.ToString());



                Guid guidDoc = Guid.NewGuid();



                documentInfom.R_Id = guidDoc.ToString();




                documentInfom.R_Title = DropDownListCourse.SelectedText.ToString();
                documentInfom.TC_Id = DropDownListCourse.SelectedValue.ToString();

                // documentInfom.T_Id = Session["UserName"].ToString(); 教学班 
                // documentInfom.R_Title = tbxCourseTitle.Text.ToString();
                documentInfom.R_Task = txareaTask.Text.ToString();
                documentInfom.R_STime = datepickerStart.Text.ToString();
                documentInfom.R_ETime = datepickeEnd.Text.ToString();
                documentInfom.R_Status = "未发布";

                documentInfom.R_Formerly = fileUploadDoc.ShortFileName.ToString();
                documentInfom.R_Path = Convert.ToInt64(document_time.TotalSeconds).ToString() + fileUploadDoc.ShortFileName.ToString();

                if (documentation.Add(documentInfom))
                {
                    Alert.Show("报告创建成功", MessageBoxIcon.Information);
                    Response.Redirect("~/template_management.aspx");
                }

            }


        }


        private string state;
        public string getState() {
            return state;
        }
        public void setState(string state) {
            this.state = state;
        }
      
    }
    
}